<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @method static withSort($sort)
 */
class GameMatch extends Model
{
    use HasFactory;

    use SoftDeletes;

    protected $fillable = [
        'type', 'start_at', 'screen', 'left_member_id', 'right_member_id', 'win_member_id',
        'nex_match_id', 'sort', 'game_id', 'loss'
    ];

    public function leftMember()
    {
        return $this->belongsTo(Member::class, 'left_member_id');
    }

    public function rightMember()
    {
        return $this->belongsTo(Member::class, 'right_member_id');
    }

    public function winMember()
    {
        return $this->belongsTo(Member::class, 'win_member_id');
    }

    public function nextMatch()
    {
        return $this->belongsTo(GameMatch::class, 'nex_match_id');
    }

    public function game()
    {
        return $this->belongsTo(Game::class);
    }

    public function scopeWithSort($query, $sort)
    {
        return $query->where('sort', $sort);
    }
}
